package ren.kane.asdppp.activeobject;

/**
 * @author renrk(1275) on 2019-04-22 3:57 PM
 **/
public class DelayedTyper implements Command {

    private long delay;

    private char itsChar;

    private static ActiveObjectEngine activeObjectEngine = new ActiveObjectEngine();

    private static boolean stop = false;

    public DelayedTyper() {
    }

    public DelayedTyper(long delay, char itsChar) {
        this.delay = delay;
        this.itsChar = itsChar;
    }

    @Override
    public void execute() throws Exception {
        System.out.print(itsChar);
        if (!stop) {
            delayAndRepeat();
        }
    }

    private void delayAndRepeat() {
        activeObjectEngine.addCommand(new SleepCommand(delay, activeObjectEngine, this));
    }

    public static void main(String[] args) throws Exception {
        activeObjectEngine.addCommand(new DelayedTyper(100, '1'));
        activeObjectEngine.addCommand(new DelayedTyper(300, '3'));
        activeObjectEngine.addCommand(new DelayedTyper(500, '5'));
        activeObjectEngine.addCommand(new DelayedTyper(700, '7'));

        Command stopCommand = () -> stop = true;
        activeObjectEngine.addCommand(new SleepCommand(20000, activeObjectEngine, stopCommand));
        activeObjectEngine.run();
    }
}
